﻿using System;
using System.IO.IsolatedStorage;

namespace Renren.Services
{
    public static class RenrenSettings
    {
        public const string ApiUriBase = "http://api.renren.com/restserver.do";

        public const string LoginRequestUri = "https://graph.renren.com/oauth/authorize?client_id=14a0da5656d54602bcfe094bd0c3feb6&redirect_uri=http://graph.renren.com/oauth/login_success.html&response_type=token&x_renew=true&display=touch&" +
                                              "scope=" +
                                              "read_user_share " +
                                              "read_user_comment " +
                                              "read_user_feed " +
                                              "status_update " +
                                              "read_user_status " +
                                              "read_user_photo " +
                                              "read_user_blog " +
                                              "publish_comment";

        public const string ApiKey = "14a0da5656d54602bcfe094bd0c3feb6";
        public const string ApiSecret = "4c3abd4d00114e49979610fb413807b9";

        public const string AccessTokenKey = "RenrenAccessToken";
        public const string NewsfeedKey = "NewsfeedXml";
        public const string LoginUserKey = "LoginUserXml";

        public static bool CacheEnabled { get; set; }

        static private string _accessToken;
        static public string AccessToken
        {
            get { return _accessToken; }
            set { _accessToken = value; }
        }

        static public bool AccessTokenIsVaild
        {
            get { return AccessToken != null && AccessToken.Length == 70; }
        }

        public static string Read(string key)
        {
            var settings = IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains(key) && settings[key] != null)
            {
                return settings[key].ToString();
            }
            return null;
        }

        public static void Write(string key, string value)
        {
            var settings = IsolatedStorageSettings.ApplicationSettings;
            if (settings.Contains(key))
            {
                settings[key] = value;
            }
            else
            {
                settings.Add(key, value);
            }
        }

        public static void AppLoaded()
        {
            AccessToken = Read(AccessTokenKey);
        }

        public static void AppUnloaded()
        {
            Write(AccessTokenKey, AccessToken);
        }

        public static bool ParseLoginReturnedUri(Uri returnedUri)
        {
            if (returnedUri.AbsolutePath == "/oauth/login_success.html")
            {
                var uri = returnedUri.ToString();
                AccessToken = uri.Substring(uri.IndexOf('=') + 1, uri.IndexOf('&') - uri.IndexOf('=') - 1);
                return true;
            }
            return false;
        }
    }
}
